<?php
declare (strict_types = 1);

namespace app\api\controller;

use Elasticsearch\ClientBuilder;
use think\Request;

class Article
{

    public function init(){
      $client = ClientBuilder::create(['127.0.0.1:9200'])->build();
        $params = [
            'index' => 'articles',
            'body' => [
                'settings' => [
                    'number_of_shards' => 3,
                    'number_of_replicas' => 2
                ],
                'mappings' => [
                    '_source' => [
                        'enabled' => true
                    ],
                    'properties' => [
                        'topic' => [
                            'type' => 'text',
                            "analyzer" => "ik_max_word",
                            "search_analyzer" => "ik_max_word"
                        ],
                        'desc' => [
                            'type' => 'text',
                            "analyzer" => "ik_max_word",
                            "search_analyzer" => "ik_max_word"
                        ]
                    ]
                ]
            ]
        ];
        $response = $client->indices()->create($params);
       return json($response);
    }

    public function add(){
        $data = \app\api\model\Article::select()->toArray();
        $client = ClientBuilder::create(['127.0.0.1:9200'])->build();
        foreach ($data as $v){
            $params = [
                'index' => 'articles',
                'type' => '_doc',
                'id' => $v['id'],
                'body' => [
                    'topic' => $v['topic'],
                    'desc' => $v['content']
                ]
            ];
           $res =  $client->index($params);
           var_dump($res);
        }
    }

    public function bool(Request $request){
        $key = input('key');
        $client = ClientBuilder::create(['127.0.0.1:9200'])->build();
        $params = [
            'index' => 'articles',
            'type' => '_doc',
            'body' => [
                'query' => [
                    'bool' => [
                        'should' => [
                            [ 'match' => [ 'topic' => $key ] ],
                            [ 'match' => [ 'desc' => $key ] ],
                        ]
                    ]
                ]
            ]
        ];
        $results = $client->search($params);
       $data = $results['hits']['hits'];
       foreach ($data as $v){
           \app\api\model\Article::where('id', $v['_id'])
               ->inc('read')
               ->update();
       }
       return json(['code'=>200,'data'=>$data]);
    }


    public function search(){
       $data = \app\api\model\Article::order('read', 'desc')
            ->limit(5)
            ->select()
            ->toArray();
        return json(['code'=>200,'data'=>$data]);
    }



    /**
     * 显示资源列表
     *
     * @return \think\Response
     */
    public function index()
    {
        //
    }

    /**
     * 显示创建资源表单页.
     *
     * @return \think\Response
     */
    public function create()
    {
        //
    }

    /**
     * 保存新建的资源
     *
     * @param  \think\Request  $request
     * @return \think\Response
     */
    public function save(Request $request)
    {
        //
    }

    /**
     * 显示指定的资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function read($id)
    {
        //
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * 保存更新的资源
     *
     * @param  \think\Request  $request
     * @param  int  $id
     * @return \think\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * 删除指定资源
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        //
    }
}
